package com.example.sunlight.interneteverything.View;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.sunlight.interneteverything.R;

/**
 * Created by SunLight on 2017/7/31.
 */

public class Views extends View {
    Bitmap one, two, three;
    Resources resources;
    Matrix matrix;
    RoteThread roteThread;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    Views.this.invalidate();
                    break;
            }
        }
    };

    public Views(Context context) {
        super(context);
        init();
        roteThread = new RoteThread();
        roteThread.start();
    }

    public void init() {
        resources = getResources();
        matrix = new Matrix();
        one = ((BitmapDrawable) resources.getDrawable(R.drawable.one)).getBitmap();
        two = ((BitmapDrawable) resources.getDrawable(R.drawable.two)).getBitmap();
        three = ((BitmapDrawable) resources.getDrawable(R.drawable.three)).getBitmap();
    }

    public Views(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Views(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setStrokeWidth(3);
        paint.setAntiAlias(true);
        paint.setColor(Color.argb(255, 19, 113, 107));

        drawRotateBitmap(canvas, paint, one, onerot, 128, 128);
        drawRotateBitmap(canvas, paint, two, tworot, 85, 85);
        drawRotateBitmap(canvas, paint, three, threerot, 0, 0);
    }

    int onerot = 0;
    int tworot = 0;
    int threerot = 0;


    private void drawRotateBitmap(Canvas canvas, Paint paint, Bitmap bitmap,
                                  float rotation, float posX, float posY) {
        Matrix matrix = new Matrix();
        int offsetX = bitmap.getWidth() / 2;
        int offsetY = bitmap.getHeight() / 2;
        matrix.postTranslate(-offsetX, -offsetY);
        matrix.postRotate(rotation);
//        matrix.postScale(0.75f, 0.75f);
        matrix.postTranslate(posX + offsetX, posY + offsetY);
        canvas.drawBitmap(bitmap, matrix, paint);
    }

    boolean state = false;

    class RoteThread extends Thread {

        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(75);
                    if (state == false) {
                        if (onerot < 30) {
                            onerot += 2;
                            tworot += 2;
                            threerot += 2;
                            handler.sendEmptyMessage(0x123);
                        } else {
                            state = true;
                        }
                    } else {
                        if (state == true) {
                            if (onerot > -30) {
                                onerot -= 2;
                                tworot -= 2;
                                threerot -= 2;
                                handler.sendEmptyMessage(0x123);
                            } else {
                                state = false;
                            }
                        }
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
